package com.link2room.optimus.facade.impl;

import com.link2room.common.util.JacksonUtil;
import com.link2room.optimus.dto.param.manager.L2RRateplanManagerParamDto;
import com.link2room.common.entity.BaseEntity;
import com.link2room.common.jpa.condition.ConditionDetail;
import com.link2room.common.jpa.condition.L2RCondition;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.optimus.entity.L2RRateplan;
import com.link2room.optimus.constant.BaseRateplanConstant;
import com.link2room.optimus.dto.result.base.L2RCancelRuleDto;
import com.link2room.optimus.dto.result.base.L2RRateplanDto;
import com.link2room.optimus.facade.IL2RRateplanFacadeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by sss on 2016/9/1.
 */
@Component("l2rRateplanFacadeService")
public class L2RRateplanFacadeServiceImpl implements IL2RRateplanFacadeService {
    @Resource
    private ICommonService commonService;

    /**
     * 保存价格计划
     * @param l2RRateplanManagerParamDto
     * @throws Exception
     */
    @Override
    public L2RRateplanDto saveL2RRateplan(L2RRateplanManagerParamDto l2RRateplanManagerParamDto)throws Exception{
        L2RRateplan l2rRateplan = ClassConverUtil.copyProperties(l2RRateplanManagerParamDto.getL2rRateplan(), L2RRateplan.class);
        if(BaseRateplanConstant.CANCELTYPE.CUSTOM.equals(l2RRateplanManagerParamDto.getL2rRateplan().getCancelType())) {
            l2rRateplan.setCancelRule(JacksonUtil.beanToJson(l2RRateplanManagerParamDto.getL2rRateplan().getL2rCancelRuleDto()));
        }
        commonService.save(l2rRateplan);
        L2RRateplanDto l2RRateplanDto = l2RRateplanManagerParamDto.getL2rRateplan();
        l2RRateplanDto.setId(l2rRateplan.getId());
        return l2RRateplanDto;
    }

    /**
     * 修改价格计划
     * @param l2RRateplanManagerParamDto
     * @throws Exception
     */
    @Override
    public L2RRateplanDto updateL2RRateplan(L2RRateplanManagerParamDto l2RRateplanManagerParamDto)throws Exception{
        L2RRateplan l2rRateplan = commonService.findOneById(L2RRateplan.class, l2RRateplanManagerParamDto.getL2rRateplan().getId());
        if(l2rRateplan!=null){
            ClassConverUtil.copyProperties(l2RRateplanManagerParamDto.getL2rRateplan(), l2rRateplan);
            if(BaseRateplanConstant.CANCELTYPE.CUSTOM.equals(l2RRateplanManagerParamDto.getL2rRateplan().getCancelType())) {
                l2rRateplan.setCancelRule(JacksonUtil.beanToJson(l2RRateplanManagerParamDto.getL2rRateplan().getL2rCancelRuleDto()));
            }
        }
        commonService.merge(l2rRateplan);
        L2RRateplanDto l2RRateplanDto = l2RRateplanManagerParamDto.getL2rRateplan();
        l2RRateplanDto.setId(l2rRateplan.getId());
        return l2RRateplanDto;
    }

    /**
     * 价格计划列表
     * @param l2RRateplanManagerParamDto
     * @throws Exception
     */
    @Override
    public QueryResult<L2RRateplanDto> listL2RRateplans(L2RRateplanManagerParamDto l2RRateplanManagerParamDto)throws Exception{
        L2RCondition l2RCondition=new L2RCondition();
        ConditionDetail[] conditionDetails = new ConditionDetail[2];
        conditionDetails[0]= new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ, l2RRateplanManagerParamDto.getLhotelGroupCode());
        if(StringUtil.isNotBlank(l2RRateplanManagerParamDto.getMcCode())){
            conditionDetails[1]= new ConditionDetail(L2RRateplan.L2RRateplan_.rpCode.toString(), CriteriaUtil.Operator.LIKE, l2RRateplanManagerParamDto.getMcCode() + "%");
        }else{
            conditionDetails[1]= new ConditionDetail(L2RRateplan.L2RRateplan_.rpCode.toString(), CriteriaUtil.Operator.EQ, l2RRateplanManagerParamDto.getRpCode() );
        }
        l2RCondition.setAndConditions(conditionDetails);

        QueryResult<L2RRateplan> l2rRateplanResult = commonService.findAllByCondition(L2RRateplan.class,
                l2RCondition, l2RRateplanManagerParamDto.getCurrentPage(), l2RRateplanManagerParamDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(l2rRateplanResult, L2RRateplanDto.class, false);
    }

    /**
     * 单个价格计划
     * @param l2RRateplanManagerParamDto
     * @throws Exception
     */
    @Override
    public L2RRateplanDto getL2RRateplanById(L2RRateplanManagerParamDto l2RRateplanManagerParamDto)throws Exception{
        L2RRateplan l2rRateplan = commonService.findOneById(L2RRateplan.class, l2RRateplanManagerParamDto.getId());
        L2RRateplanDto l2rRateplanDto = ClassConverUtil.copyPropertiesToDto(l2rRateplan, L2RRateplanDto.class, false);
        l2rRateplanDto.setL2rCancelRuleDto(JacksonUtil.jsonToBean(l2rRateplan.getCancelRule(),L2RCancelRuleDto.class));
        return l2rRateplanDto;
    }

    /**
     * 单个价格计划
     * @param l2RRateplanManagerParamDto
     * @throws Exception
     */
    @Override
    public L2RRateplanDto getL2RRateplanByRpcode(L2RRateplanManagerParamDto l2RRateplanManagerParamDto)throws Exception{
        L2RRateplan queryLRP=new L2RRateplan();
        queryLRP.setLhotelGroupCode(l2RRateplanManagerParamDto.getLhotelGroupCode());
        queryLRP.setRpCode(l2RRateplanManagerParamDto.getRpCode());
        L2RRateplan l2rRateplan = commonService.findOneEQ(L2RRateplan.class, queryLRP);
        L2RRateplanDto l2rRateplanDto = ClassConverUtil.copyPropertiesToDto(l2rRateplan, L2RRateplanDto.class, false);
        if(l2rRateplan!=null){
            if(StringUtil.isNotBlank(l2rRateplan.getCancelRule())){
                l2rRateplanDto.setL2rCancelRuleDto(JacksonUtil.jsonToBean(l2rRateplan.getCancelRule(),L2RCancelRuleDto.class));
            }
        }
        return l2rRateplanDto;
    }


}
